Move notebook to /lib
[and.git] / Andy's big heart / heart.cpp
blob9868ced34adbe5369dcaeb7f49598f04cb770569
1 #include <math.h>
2 #include <iostream>
3 #include <algorithm>
4 #include <cassert>
6 #define EPSILON 0.000001
8 using namespace std;
10 struct Point{
11 double x, y;
12 bool operator < (const Point &p) const {
13 return (x < p.x || (x == p.x && y < p.y));
16 double dist(const Point &p) const {
17 return hypot(p.x-x, p.y-y);
20 void print(){
21 cout << "("<<x<<","<<y<<")\n";
25 struct Line{
26 double a;
27 double b;
28 double c;
29 void makePointSlopeLine(Point p, double m){
30 a = -m;
31 b = 1;
32 c = -((a*p.x) + (b*p.y));
34 bool parallel(const Line &l) const {
35 return ((fabs(a - l.a) <= EPSILON) && (fabs(b - l.b) <= EPSILON));
38 bool equals (const Line &l) const {
39 return (parallel(l) && (fabs(c-l.c) <= EPSILON));
42 Point intersection(const Line &l) const {
43 assert( !(equals(l)) );
44 assert( !(parallel(l)));
45 Point r;
46 r.x = (l.b*c - b*l.c) / (l.a*b - a*l.b);
47 if (fabs(b) > EPSILON){
48 r.y = - (a * r.x + c) / b;
49 }else{
50 r.y = - (l.a * r.x + l.c) / l.b;
52 return r;
56 int main(){
57 Point p[3];
58 for (int i=0; i<3; ++i){
59 int x, y;
60 cin >> x >> y;
61 p[i].x = x;
62 p[i].y = y;
64 while (p[0].x != 0 || p[0].y != 0 || p[1].x != 0 || p[1].y != 0 || p[2].x != 0 || p[2].y != 0){
65 //Procesar caso aquĆ­
66 sort(p, p+3);
68 // for (int i=0; i<3; ++i){
69 // p[i].print();
70 // }
73 Point pm1, pm2;
74 pm1.x = (p[0].x + p[1].x) / 2.0;
75 pm1.y = (p[0].y + p[1].y) / 2.0;
77 pm2.x = (p[1].x + p[2].x) / 2.0;
78 pm2.y = (p[1].y + p[2].y) / 2.0;
81 double m1, m2;
82 m1 = (p[0].y - p[1].y)/(p[0].x - p[1].x);
83 m2 = (p[1].y - p[2].y)/(p[1].x - p[2].x);
85 double pmm1 = -1/m1;
86 double pmm2 = -1/m2;
87 //cout << pmm1<<" "<<pmm2<<endl;
89 Line s1, s2;
90 s1.makePointSlopeLine(pm1, pmm1);
91 s2.makePointSlopeLine(pm2, pmm2);
93 //if (s1.parallel(s2)){
94 if (fabs(p[0].x*(p[1].y - p[2].y) +
95 p[1].x*(p[2].y - p[0].y) +
96 p[2].x*(p[0].y - p[1].y)) < EPSILON){
97 cout << "Andy's big heart will break.\n";
98 }else{
99 Point intersection = s1.intersection(s2);
100 assert(intersection.dist(p[0]) - intersection.dist(p[1]) <= EPSILON);
101 assert(intersection.dist(p[1]) - intersection.dist(p[2]) <= EPSILON);
102 assert(intersection.dist(p[2]) - intersection.dist(p[0]) <= EPSILON);
103 //cout << intersection.dist(p[0])<<endl;
104 //cout << intersection.dist(p[1])<<endl;
105 //cout << intersection.dist(p[2])<<endl;
107 printf("Andy should move to the house at point (%.3f, %.3f).\n", intersection.x, intersection.y);
113 //leer puntos
114 for (int i=0; i<3; ++i){
115 int x, y;
116 cin >> x >> y;
117 p[i].x = x;
118 p[i].y = y;
121 return 0;